<?php
/**
 * 单条录入采购单
 */
class Controller_Uploadpay extends Controller_Admin
{
	public function before()
    {
    	try {
    		  ACL::Required('uploadpay');
    	} catch ( HTTP_Exception_403 $e ) {
				Message::error('sorry，你没有权限进这个目录');
    		$this->request->redirect ( URL::base (true) );
    	}
        parent::before();
    }
    
    /**
     * 
     */
    public function action_payshow()
    {
    	$purchaseorder_id = $_GET['purchaseorder_id'];
		$sql    = "SELECT * FROM `purchaseorders` WHERE `id` = ".$purchaseorder_id;
		$purchaseorder_info = DB::query(Database::SELECT,$sql)->execute('newerp')->current();
		if(!isset($purchaseorder_info['id'])){
			echo  json_encode(0);exit;
		}
		//if(($purchaseorder_info['status']<20) || ($purchaseorder_info['status']>90)){
		if($purchaseorder_info['status']<20){
			echo  json_encode(1);exit;
		}
//		$purchase = Config::load('purchase_status');
//		$purchase_status = $purchase->purchaseorder_details;
//		$purchaseorder = $purchase->purchaseorder;
//		$purchaseorder_info['status'] = $purchaseorder[$purchaseorder_info['status']];
//		$purchaseorder_detail = Purchase::change_purchase($purchaseorder_id);
//		foreach($purchaseorder_detail as $key=>&$value)
//		{
//			$value['status'] = $purchase_status[$value['status']];
//		}
		$supplier=Purchase::select_s_name($purchaseorder_info['supplier_id']);
		$pay_ways = Service_warning::get_pay_ways();
		foreach ($pay_ways as $pay_way){
			if($pay_way['id']==$purchaseorder_info['pay_way']){
				$payway=$pay_way['pay_way'];
			}
		}
		$view=View::factory('money/singleupload')
			->set('supplier',$supplier)
			->set('payway',$payway)
			->set('purchaseorder_info',$purchaseorder_info)
 			->render();
 		$jsonarr=array('status'=>'y','info'=>'数据查询成功！','view'=>$view);
    	echo json_encode($jsonarr);
    }
    
    /**
     * 修改采购付款单
     */
    public function action_payupdate()
    {
    	$userid = User::active_user()->id;
    	$purchaseorder_id=intval($_POST['purchaseorder_id']);
		$purchaseorder = ORM::factory('purchaseorder')
			->where('id', '=', $purchaseorder_id)
			->where('status', '>=', '20')
			->where('status', '<=', '100')->find();
		if($purchaseorder->id!=''){
	    	$ok=ORM::factory('purchaseorder_pay');
			$ok->purchaseorder_id=$purchaseorder_id;
			$ok->supplier_id=$purchaseorder->supplier_id;
			$ok->pay_way=$purchaseorder->pay_way;
			$ok->pay_date=date('Y-m-d H:i:s');
			$ok->payer=$userid;
			$ok->amount_paid=$_POST['amount_paid'];
			$ok->remark=$_POST['remark'];
			$ok->save();
			echo json_encode(1);exit;
		}else{
			//采购单未打印或者已取消
			echo json_encode(0);exit;
		}
    }
}
